package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Models.Documents;
import Models.InventoryUsage;
import Models.VesselCleaningProcedure;

public class VesselCleaningProcedurePresenter extends AbstractPresenter<VesselCleaningProcedure> {
	final String SINGLEITEM = "vesselCleaningProcedure";
	final String MULTIITEM = "vesselCleaningProcedures";
	
	public ArrayList<VesselCleaningProcedure> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public VesselCleaningProcedure tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public VesselCleaningProcedure createItemFromJSONObject(JSONObject jsonObject){
		VesselCleaningProcedure item = new VesselCleaningProcedure();
		try{
			item.setVcpID(jsonObject.getInt("vcpid"));
			item.setVcpRepeatDays(jsonObject.getInt("VCPRepeatDays"));
			item.setVcpProcedure(jsonObject.getString("VCPProcedure"));
			item.setVcpDescription(jsonObject.getString("VCPDesc"));
			item.setMen(jsonObject.getInt("taskMen"));
			item.setMinutes(jsonObject.getInt("taskMinutes"));
			item.setRateType(jsonObject.getInt("rateType"));
			//Set Documents Collection
			ArrayList<Documents> documents = new ArrayList<Documents>();
			if(!jsonObject.isNull("documentsCollection")){
				Object itm = jsonObject.get("documentsCollection");
				DocumentPresenter presenter = new DocumentPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						documents.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					documents.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setDocs(documents);
			}
			//Set Inventory Usage Collection
			ArrayList<InventoryUsage> invUsages = new ArrayList<InventoryUsage>();
			if(!jsonObject.isNull("invUsageCollection")){
				Object itm = jsonObject.get("invUsageCollection");
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						invUsages.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					invUsages.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setInventoryUsages(invUsages);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(VesselCleaningProcedure item){
		try{
			JSONObject json = new JSONObject();
			json.put("vcpid", item.getVcpID());
			json.put("VCPDesc", item.getVcpDescription());
			json.put("VCPProcedure", item.getVcpProcedure());
			json.put("VCPRepeatDays", item.getVcpRepeatDays());
			json.put("taskMen", item.getMen());
			json.put("taskMinutes", item.getMinutes());
			json.put("rateType", item.getRateType());
			//Create Doc Collection
			if(item.getDocs()!=null){
				DocumentPresenter presenter = new DocumentPresenter();
				if(item.getDocs().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getDocs().size(); i++){
						jsonArray.put(presenter.createJSON(item.getDocs().get(i)));
					}
					json.put("documentsCollection", jsonArray);
				}
			}
			//Create Doc Collection
			if(item.getInventoryUsages()!=null){
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(item.getInventoryUsages().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getInventoryUsages().size(); i++){
						jsonArray.put(presenter.createJSON(item.getInventoryUsages().get(i)));
					}
					json.put("invUsageCollection", jsonArray);
				}
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public VesselCleaningProcedure getEmptyEntity(){
		VesselCleaningProcedure entity = new VesselCleaningProcedure();
		return entity;
	}
}
